/*键盘输入10组商品信息,包括产地,名称,保存到d:prod.txt里.
 * 键盘输入一个地名,读取文件,找到所有此地生产的商品信息并输出.
 */
 
 /*1.利用每条记录中的分隔符，进行拆分并拼接,注意Remove the '\n',at the end of each record.
  *2.注意拼接时,间隔的字节数,指针很好用，可以精确到每个字节.
  *3.中文记录运行正常.
  *4.体会到最简易的搜索方法.
  * 5.C语言的拼接方法不如python中的更易于直接使用,尤其是python进行多级dict的层序遍历时。
  * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct product_st
{
    char name[20];
    char place[20];
}Prdct;

int main(int argc, char *argv[])
{
    enum {N = 4};
    Prdct prdt[N];
    int i;
    
    FILE *fp;
    fp = fopen("D:\\prod.txt","w");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    for(i = 0; i < N; i++)
    {
        puts("\nEnter product name and place:\n");
        gets(prdt[i].name);
        fwrite(&prdt[i].name,strlen(prdt[i].name),1,fp); 
        fputc(',',fp);
        gets(prdt[i].place);
        fwrite(&prdt[i].place,strlen(prdt[i].place),1,fp);
        fputc('\n',fp);
    }
    
    fclose(fp);
    puts("Save file successfully!\n");
    
    // Read file
    fp = fopen("D:\\prod.txt","r");
    
    if(fp == NULL)
    {
        fprintf(stderr,"open() failed!");
        exit(1);
    }
    
    char record[256]= {'\0'};
    
    //Find product name according place.
    // Read one line.
    char pname[20];
    char pplace[20];
    char tmp[20];
    char result[256];
    char *p = result;
    int idx;
    int cnt = 0;
    puts("Enter place like[USA,UK]");
    gets(tmp);
    //tmp[strlen(tmp)+1] = '\0';

    while(fgets(record,256,fp))
    {    
        //read one record to name.
        //fscanf(fp,"%[^\n]",name);
        //fscanf(fp,"%[^\n]",name);
        for(i = 0; i < strlen(record); i++)
        {
            if(record[i] == ',')
            {
                 idx = i;
            }
        }
        //get product name.
        for(i = 0; i < idx; i++)
        {
            *(pname + i) = record[i];
        }
        // Notice.
        pname[i] = '\0';
        //get product place.
        strcpy(pplace, &record[idx+1]);
        //remove the '\n',at the end.
        pplace[strlen(pplace)-1]= '\0';
        printf("pname = %s place = %s\n",pname,pplace);
        if((strcmp(tmp,pplace)) == 0)
        {
           strcpy(p,pname);
           puts(pname);
           puts(p);
           puts("strcpy\n");
           //address increae one pname bytes
           p = p + strlen(pname);
        }
        
        //puts(tmp);
        //puts(pplace);
//        for(i = 0; i < tmp[i] != '\0'; i++)
//        {
//            if(tmp[i] == pplace[i])
//            {
//                cnt++;
//            }
//            else
//            {
//                break;
//            }
//        }
//        if(cnt == strlen(pplace))
//        {
//            puts(tmp);
//            puts(pplace);
//        }
    }
    
    p[strlen(p)+1] = '\0';
    puts("\n");
    puts(result);
//    char tmp[20]= {'\0'};
//    
//    puts("Enter place like[USA,UK]");
//    gets(tmp);
//    for(i = 0; i < N; i++)
//    {
//        if((fread(&prdt[i],sizeof(prdt[i]),1,fp)) > 0)
//        {
//            if(strcmp(tmp,prdt[i].place) == 0)
//            {
//                strcpy(name,prdt[i].name);
//            }
//        }
//        
//    }
//    
//    printf("%s \n", name);
    puts("Hello, World!");
    
    fclose(fp);
    return 0;
}
